package com.tianxin.workbao.workBao.dataProvide;

import android.content.Context;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.CarFinance;
import com.tianxin.workbao.workBao.bean.CarFinanceDao;
import com.tianxin.workbao.workBao.bean.CarInformationDao;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.common.URL;
import com.tianxin.workbao.workBao.helper.GreenDaoHelper;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.okhttp.CommonOkHttpClient;
import com.tianxin.workbao.workBao.okhttp.exception.OkHttpException;
import com.tianxin.workbao.workBao.okhttp.listener.DisposeDataHandle;
import com.tianxin.workbao.workBao.okhttp.listener.DisposeDataListener;
import com.tianxin.workbao.workBao.okhttp.request.CommonRequest;
import com.tianxin.workbao.workBao.okhttp.request.RequestParams;
import com.tianxin.workbao.workBao.utils.common.BeansUtil;
import com.tianxin.workbao.workBao.utils.common.NetworkUtils;

import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.WhereCondition;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hacker on 2017/7/14.
 */

public class CarFinanceProvide extends BaseDataProvide {

    private CarFinance mCarFinance;
    private String plateOrCpuNumber;
    private UpdateListViewListener mUpdateListViewListener;
    private CarFinanceDao carFinanceDao;


    public CarFinanceProvide(Context context, UpdateListViewListener listener) {
        super(context);
        mUpdateListViewListener = listener;
        carFinanceDao = GreenDaoHelper.newInstance().getDaoSession().getCarFinanceDao();
    }


    @Override
    public DisposeDataHandle getDisposeDataHandle() {
        disposeDataHandle = new DisposeDataHandle(getDisposeDataListener());
        return disposeDataHandle;
    }

    @Override
    public DisposeDataListener getDisposeDataListener() {
        disposeDataListener = new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                mUpdateListViewListener.hideProgress();
                try {
                    JSONObject object = new JSONObject(responseObj.toString());
                    int status = object.getInt(Constant.RESPONSE_STATUS);
                    String information = object.getString(Constant.RESPONSE_INFO);
                    if (status > 0) {
                        String dataJson = object.getString(Constant.RESPONSE_DATA);
                        if (dataJson != null || dataJson.length() > 2) {
                            try {
                                mCarFinance = BeansUtil.carFinanceToBean(dataJson);
                                refreshUI();
                            } catch (Exception e) {
                                mUpdateListViewListener.showMessage("car finance To Bean Error :类型转换出错" + e.getMessage());
                            }

                        } else {
                            mUpdateListViewListener.showMessage(information);
                        }
                    } else {
                        mUpdateListViewListener.showMessage(information);
                    }

                } catch (JSONException e) {
                    showRequestErrorMessage(mUpdateListViewListener, responseObj.toString());
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Object reasonObj) {
                mUpdateListViewListener.hideProgress();
                if (reasonObj instanceof OkHttpException) {
                    OkHttpException exception = (OkHttpException) reasonObj;
                    mUpdateListViewListener.showMessage(exception.getEcode() + ":" + exception.getEmsg().toString());
                } else {
                    showRequestErrorMessage(mUpdateListViewListener, reasonObj.toString());
                }
            }
        };
        return disposeDataListener;
    }



    /**
     * get car finance
     */
    public void getCarfinance(String queryString) {
        plateOrCpuNumber = queryString;
        if (!NetworkUtils.isConnected(mContext)) {
            mUpdateListViewListener.showMessage(mContext.getResources().getString(
                    R.string.poor_network
            ));
            return;
        }
        getFromServer();
    }

    /**
     * update the coal data  from server
     */
    private void getFromServer() {
        mUpdateListViewListener.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put(Constant.APPTOKEN_NAME, getAppToken());
        map.put("plate", plateOrCpuNumber);
        RequestParams params = new RequestParams(map);
        CommonOkHttpClient.post(CommonRequest.createPostRequest(URL.getCarFianceUrl(), params), getDisposeDataHandle());
    }

    @Override
    public void refreshUI() {
        mUpdateListViewListener.onRefreshData(mCarFinance);
    }

}
